/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.worker.ui.async.gui;

import org.ourgrid.common.interfaces.status.WorkerCompleteStatus;
import org.ourgrid.common.ui.OurGridUIModel;

import br.edu.ufcg.lsd.commune.identification.DeploymentID;

/**
 * It represents a panel where it is possible to view the status of the worker.
 * @author  Diego
 */
public class WorkerStatusPanel extends javax.swing.JPanel {
    
	private static final long serialVersionUID = 1L;
	
	private OurGridUIModel model;

	/** Creates new form WorkerStatusPanel
     * @param model The model of the worker.
     */
    public WorkerStatusPanel(OurGridUIModel model) {
    	this.model = model;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        workerStatusLabel = new javax.swing.JLabel();
        masterPeerIdLabel = new javax.swing.JLabel();
        playpenDirLabel = new javax.swing.JLabel();
        storageDirLabel = new javax.swing.JLabel();
        workerStatusTextField = new javax.swing.JTextField();
        masterPeerIDTextField = new javax.swing.JTextField();
        playpenDirTextField = new javax.swing.JTextField();
        storageDirTextField = new javax.swing.JTextField();

        workerStatusLabel.setText("Worker Status:");

        masterPeerIdLabel.setText("Master Peer ID:");

        playpenDirLabel.setText("Playpen Dir:");
        
        storageDirLabel.setText("Storage Dir");
        
        workerStatusTextField.setEditable(false);
        
        masterPeerIDTextField.setEditable(false);
        
        playpenDirTextField.setEditable(false);
        
        storageDirTextField.setEditable(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(storageDirLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        	.add(playpenDirLabel)
                        	.add(masterPeerIdLabel)
                            .add(workerStatusLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(workerStatusTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .add(masterPeerIDTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .add(playpenDirTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .add(storageDirTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))))
                .addContainerGap(89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(workerStatusLabel)
                    .add(workerStatusTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(masterPeerIdLabel)
                    .add(masterPeerIDTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(playpenDirLabel)
                    .add(playpenDirTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(storageDirLabel)
                    .add(storageDirTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel playpenDirLabel;
    private javax.swing.JTextField playpenDirTextField;
    private javax.swing.JLabel storageDirLabel;
    private javax.swing.JTextField storageDirTextField;
    private javax.swing.JLabel masterPeerIdLabel;
    private javax.swing.JTextField masterPeerIDTextField;
    private javax.swing.JLabel workerStatusLabel;
    private javax.swing.JTextField workerStatusTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * Defines the worker status.
     * @param workerStatus The worker status.
     */
    public void setWorkerStatus(String workerStatus){
    	workerStatusTextField.setText(workerStatus);
    }
    
    /**
     * Defines the master peer ID.
     * @param masterPeerID The master peer ID.
     */
    public void setMasterPeerID(String masterPeerID){
    	masterPeerIDTextField.setText(masterPeerID);
    }
    
    /**
     * Defines the consumer ID.
     * @param consumerID The consumer ID.
     */
    public void setConsumerID(String consumerID){
    	playpenDirTextField.setText(consumerID);
    }

	public void updateCompleteStatus(WorkerCompleteStatus completeStatus) {
		workerStatusTextField.setText(completeStatus.getStatus().toString());
		DeploymentID masterPeerID = completeStatus.getMasterPeer();
		if ( masterPeerID != null ) {
			masterPeerIDTextField.setText(masterPeerID.getServiceID().toString());			
		}
		playpenDirTextField.setText(completeStatus.getCurrentPlaypenDirPath());
		storageDirTextField.setText(completeStatus.getCurrentStorageDirPath());
	}
	
	public void clearFields() {
		workerStatusTextField.setText("");
		masterPeerIDTextField.setText("");			
		playpenDirTextField.setText("");
		storageDirTextField.setText("");
	}

    
}
